package utils

import (
	"net/http"
	"time"

	"github.com/getkin/kin-openapi/openapi3"
)

// AuthStateManagerInterface defines the interface for managing authentication states
type AuthStateManagerInterface interface {
	SetAuthState(authType string, state *AuthState) error
	GetAuthState(authType string) (*AuthState, bool)
	ClearAuthState(authType string)
	IsExpired(authType string) bool
	GetActiveTokens() map[string]string
	GetAllAuthStates() map[string]*AuthState
	HasAuthType(authType string) bool
	CleanupExpiredStates() int
	GetAuthStateCount() int
	Clear()
	UpdateAuthState(authType string, updater func(*AuthState) error) error
}

// AuthConfigParserInterface defines the interface for parsing authentication configurations
type AuthConfigParserInterface interface {
	ParseAuthConfig(operation *openapi3.Operation) (*AuthConfig, error)
	IsLoginEndpoint(operation *openapi3.Operation) bool
	RequiresAuth(operation *openapi3.Operation) []string
	ValidateAuthConfig(config *AuthConfig) error
}

// TokenExtractorInterface defines the interface for extracting tokens from responses
type TokenExtractorInterface interface {
	ExtractToken(responseBody []byte, config *AuthConfig) (*AuthState, error)
	ExtractFromJSONPath(data []byte, path string) (string, error)
	ParseExpiration(data []byte, path string) (*time.Time, error)
	ValidateTokenFormat(token string, format string) error
}

// AuthHeaderInjectorInterface defines the interface for injecting authentication headers
type AuthHeaderInjectorInterface interface {
	InjectAuthHeaders(req *http.Request, authStates map[string]*AuthState, config *AuthConfig) error
	InjectBearerToken(req *http.Request, token string)
	InjectAPIKey(req *http.Request, headerName, token string)
	InjectQueryParam(req *http.Request, paramName, token string)
	InjectCustomAuth(req *http.Request, config *AuthConfig, token string) error
}

// AuthValidatorInterface defines the interface for validating authentication-related data
type AuthValidatorInterface interface {
	ValidateAuthState(state *AuthState) error
	ValidateAuthConfig(config *AuthConfig) error
	ValidateTokenFormat(token, format string) error
	ValidateJSONPath(path string) error
}

// AuthSerializerInterface defines the interface for serializing/deserializing auth data
type AuthSerializerInterface interface {
	SerializeAuthState(state *AuthState) ([]byte, error)
	DeserializeAuthState(data []byte) (*AuthState, error)
	SerializeAuthConfig(config *AuthConfig) ([]byte, error)
	DeserializeAuthConfig(data []byte) (*AuthConfig, error)
}

// AuthLoggerInterface defines the interface for authentication-related logging
type AuthLoggerInterface interface {
	LogAuthSuccess(authType, sessionID string)
	LogAuthFailure(authType, sessionID, reason string)
	LogTokenExtraction(authType, sessionID string, success bool)
	LogTokenExpiration(authType, sessionID string)
	LogAuthConfigParsing(operationID string, success bool)
	LogDebug(message string, fields map[string]interface{})
	LogError(message string, err error, fields map[string]interface{})
}

// AuthCleanupInterface defines the interface for authentication cleanup operations
type AuthCleanupInterface interface {
	CleanupExpiredStates() int
	CleanupSessionStates(sessionID string) error
	ScheduleCleanup(interval time.Duration) error
	StopCleanup() error
}

// Ensure AuthStateManager implements the interface
var _ AuthStateManagerInterface = (*AuthStateManager)(nil)